Homework

  1. Прочесть в датафрейм df файл price.csv.
  2. Извлечь в колонку brand название бренда из колонки title.
  3. Записать уникальные бренды в словарь формата 'brand': '№ п/п', где порядковый номер начинается с единицы: {'Acer': 1, 'Asus': 2, 'HP': 3, 'Dell': 4, 'Apple': 5, 'Lenovo': 6} (функция enumerate).
  4. Используя полученный словарь, создать колонку brand_id, в которую записать соответствующее значение словаря (на основе колонки brand и ключа словаря).
  5. Создать новый датафрейм df_brands из столбцов brand_id и brand, удалив при этом дубли (метод drop_duplicates).
  6. Удалить из датафрейма df столбец brand.
  7. Сохранить в файл products.csv датафрейм df (без индексов, разделитель - ;).
  8. Сохранить в файл brands.csv датафрейм df_brands (без индексов, разделитель - ;).
In [1]:
import pandas as pd
In [2]:
df = pd.read_csv('price.csv', sep=';')
df
Out[2]:
id title
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU....
1 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0...
2 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA...
3 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1...
4 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU....
5 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ...
6 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space...
7 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0...
8 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5...
9 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7...
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK...
In [6]:
df['brand'] = df.title.str.extract('\w+ (?P<brand>\w+)')
In [7]:
df
Out[7]:
id title brand
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... Acer
1 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... Asus
2 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... HP
3 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... HP
4 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... Acer
5 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... Dell
6 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... Apple
7 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... Asus
8 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... HP
9 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... HP
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... Lenovo
In [14]:
df.brand.nunique()
Out[14]:
6
In [15]:
df.brand.unique()
Out[15]:
array(['Acer', 'Asus', 'HP', 'Dell', 'Apple', 'Lenovo'], dtype=object)
In [18]:
brands = {}
for el in enumerate(df.brand.unique(), 1):
    brands[el[1]] = el[0]
In [19]:
brands
Out[19]:
{'Acer': 1, 'Asus': 2, 'HP': 3, 'Dell': 4, 'Apple': 5, 'Lenovo': 6}
In [20]:
df['brand_id'] = df.brand.apply(lambda x: brands[x])
In [21]:
df
Out[21]:
id title brand brand_id
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... Acer 1
1 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... Asus 2
2 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... HP 3
3 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... HP 3
4 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... Acer 1
5 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... Dell 4
6 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... Apple 5
7 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... Asus 2
8 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... HP 3
9 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... HP 3
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... Lenovo 6
In [24]:
df_brands = df.loc[:, ['brand_id', 'brand']].drop_duplicates()
In [25]:
df_brands
Out[25]:
brand_id brand
0 1 Acer
1 2 Asus
2 3 HP
5 4 Dell
6 5 Apple
10 6 Lenovo
In [27]:
df.drop(columns=['brand'], inplace=True)
In [28]:
df
Out[28]:
id title brand_id
0 1 Ноутбук Acer Aspire 5 A515-54G-502N (NX.HVGEU.... 1
1 2 Ноутбук Asus ROG Strix G15 G512LI-HN057 (90NR0... 2
2 3 Ноутбук HP Pavilion Gaming 15-bc504ur (7DT87EA... 3
3 4 Ноутбук HP Pavilion Notebook 15-cw1011ua (8RW1... 3
4 5 Ноутбук Acer Aspire 7 A715-41G-R7MZ (NH.Q8LEU.... 1
5 6 Ноутбук Dell Inspiron 3582 (I3582C54H5NIL-BK) ... 4
6 7 Ноутбук Apple MacBook Air 13" 256GB 2020 Space... 5
7 8 Ноутбук Asus ROG Strix G15 G512LI-HN094 (90NR0... 2
8 9 Ноутбук HP Pavilion Notebook 15-cw1002ua (7KE5... 3
9 10 Ноутбук HP Pavilion Notebook 15-cw1005ua (7ZF7... 3
10 11 Ноутбук Lenovo IdeaPad L340-15IRH Gaming (81LK... 6
In [29]:
df.to_csv('products.csv', index=False, sep=';')
In [30]:
df_brands.to_csv('brands.csv', index=False, sep=';')
In [ ]: